<?php

namespace app\server\notice\model;

use app\common\model\Db;

class NoticeLog
{

    private $_table = 'log';

    /**
     * 添加发送日志
     *
     * @param array $params
     * @return int|mixed|string
     */
    public function add(array $params)
    {
        $info = [];
        $info['tem'] = $params['tem'];
        $info['content'] = json_encode($params['content']);
        $info['app_id'] = $params['app_id'] ?? '';
        $info['createtime'] = time();
        $info['updatetime'] = time();
        $info['status'] = 0;
        $info['raw'] = json_encode($params);
        if (!empty($params['mobile'])) {
            $info['mobile'] = $params['mobile'];
        } else {
            $info['email'] = $params['to'];
        }
        $database = Db::instance();
        $database->insert($this->_table, $info);
        return $database->id();
    }

    /**
     * 更新发送日志状态
     *
     * @param int $id
     * @param $status
     * @param string $errorMsg
     * @return bool|\PDOStatement
     */
    public function modify(int $id, $status, string $errorMsg = '')
    {
        $database = Db::instance();
        return $database->update($this->_table, ['status' => $status, 'error_msg' => $errorMsg, 'updatetime' => time()], ['id' => $id]);
    }

}